package com.aimee.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aimee.dao.LoginDAO;
import com.aimee.entity.User;
import com.aimee.util.PageBean;

/**
 * 用户的管理
 */
@WebServlet("/admin/student")
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	LoginDAO dao = new LoginDAO();

	// 查一页 http://localhost:8080/OnlineExam/admin/student?m=page&p=1  p为页码
	protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
       System.out.println("查一页数据");
       String p=request.getParameter("p");
       List userlist=null;
       PageBean pd=new PageBean();
       if(p==null) {
    	   p="1";
       }
       int intp=Integer.parseInt(p);
       userlist=dao.show(intp);
       int count=(int)dao.count();
       pd.setTotal(count);
       pd.setCurPage(intp);
       //传给jsp-->request
       request.setAttribute("apage", userlist);
       request.setAttribute("pb", pd);
       request.getRequestDispatcher("/admin/student.jsp").forward(request, response);
       
	}
//单个用户导入增加
//	protected void singeradd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
//		 System.out.println("user add1");
//		 int user_id=Integer.parseInt(request.getParameter("user_id"));
//		 int class_id=Integer.parseInt(request.getParameter("class_id"));
//		 int role=Integer.parseInt(request.getParameter("role"));
//		 String name=request.getParameter("name");
//		 String password=request.getParameter("password");
//		 String phone=request.getParameter("phone");
//		 String sex=request.getParameter("sex");
////		 String wechat=request.getParameter("wechat");
//		 User user=new User(user_id,class_id,role,name,password,phone,sex);
//		 int n=dao.singeradd(user);
//		 //跳转需要写完整
//		 response.sendRedirect("http://localhost:8080/OnlineExam/admin/student?m=page");
//		 System.out.println(n);
//
//	}
//批量导入增加
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		 System.out.println("user add2");
//		 int user_id=Integer.parseInt(request.getParameter("user_id"));
		 int class_id=Integer.parseInt(request.getParameter("class_id"));
//		 int role=Integer.parseInt(request.getParameter("role"));
		 String name=request.getParameter("name");
		 String password=request.getParameter("password");
		 String phone=request.getParameter("phone");
		 String sex=request.getParameter("sex");
//		 String wechat=request.getParameter("wechat");
		 User user=new User(class_id,name,password,phone,sex);
		 int n=dao.add(user);
		 response.sendRedirect("http://localhost:8080/OnlineExam/admin/student?m=page");
		 System.out.println(n);
		
	}
//用户更新
//	protected void userUpdate(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		 System.out.println("user update");
//		 response.setContentType("text/html");
//	        response.setCharacterEncoding("UTF-8");
//	        request.setCharacterEncoding("UTF-8");
//		 int user_id=Integer.parseInt(request.getParameter("user_id"));
//		 int class_id=Integer.parseInt(request.getParameter("class_id"));
//		 int role=Integer.parseInt(request.getParameter("role"));
////		 String name=request.getParameter("name");
//		 String password=request.getParameter("password");
//		 String phone=request.getParameter("phone");
//		 String sex=request.getParameter("sex");
//		   String sex2=new String(sex.getBytes("ISO-8859-1"),"utf-8");
//		 System.out.println(sex);
//		 System.out.println(sex2);
//		 String wechat=request.getParameter("wechat");
//		 System.out.println(user_id+"--"+class_id+"--"+role+"--"+password+"--"+phone+"--"+sex2+"--"+wechat);
//		 User user=new User(user_id,class_id,role,password,phone,sex2,wechat);
//		 try {
//			int n=dao.userUpdate(user);
//			System.out.println(n);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		 //去哪？去查第一页
//		 response.sendRedirect("http://localhost:8080/OnlineExam/admin/student?m=page");
//	}

	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 System.out.println("user del");
		 int id=Integer.parseInt(request.getParameter("uid"));
		 try {
			dao.del(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 //去哪？去查第一页
		 response.sendRedirect("http://localhost:8080/OnlineExam/admin/student?m=page");
	}

//根据id查
	protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 System.out.println("查一个人的数据");
	}

}
